package com.gitee.wsl.security.hash

/**
 * Ketama算法，用于在一致性Hash中快速定位服务器位置
 *
 * @author looly
 * @since 5.7.20
 */
class KetamaHash {
    fun hash64(key: String): Long {
        val bKey = key.md5()
        return (((bKey[3].code and 0xFF).toLong() shl 24)
                or ((bKey[2].code and 0xFF).toLong() shl 16)
                or ((bKey[1].code and 0xFF).toLong() shl 8)
                or (bKey[0].code and 0xFF).toLong())
    }

    fun hash32(key: String): Int {
        return (hash64(key) and 0xffffffffL).toInt()
    }
}
